package com.danieldura.highrise.data
{
	
	public class ContactData implements HighriseData
	{
		public var emailAddresses:Array;
		public var phoneNumbers:Array;
		public var addresses:Array;
		public var instantMessengers:Array;
		public var webAddresses:Array;

		public function parse( data:XML ):void
		{
			emailAddresses = new Array();
			for each( var emailAddressXML:XML in data["email-addresses"]["email-address"] )
			{
				var emailAddress:EmailAddress = new EmailAddress();
				emailAddress.parse( emailAddressXML );
				emailAddresses.push( emailAddress );
			}
			
			phoneNumbers = new Array();
			for each( var phoneNumberXML:XML in data["phone-numbers"]["phone-number"] )
			{
				var phoneNumber:PhoneNumber = new PhoneNumber();
				phoneNumber.parse( phoneNumberXML );
				phoneNumbers.push( phoneNumber );
			}
			
			addresses = new Array();
			for each( var addressXML:XML in data["addresses"]["address"] )
			{
				var address:Address = new Address();
				address.parse( addressXML );
				addresses.push( address );
			}
			
			instantMessengers = new Array();
			for each( var instantMessengerXML:XML in data["instant-messengers"]["instant-messenger"] )
			{
				var instantMessenger:InstantMessenger = new InstantMessenger();
				instantMessenger.parse( instantMessengerXML );
				instantMessengers.push( instantMessenger );
			}
			
			webAddresses = new Array();
			for each( var webAddressXML:XML in data["web-addresses"]["web-address"] )
			{
				var webAddress:WebAddress = new WebAddress();
				webAddress.parse( webAddressXML );
				webAddresses.push( webAddress );
			}
			
		}
		
		public function toXML():XML
		{
			var data:XML = new XML( "<contact-data></contact-data>" );

			emailAddresses.forEach( function( emailAddress:EmailAddress, index:int, array:Array ):void
			{
				data["email-addresses"].appendChild( emailAddress.toXML() );
			} );
			phoneNumbers.forEach( function( phoneNumber:PhoneNumber, index:int, array:Array ):void
			{
				data["phone-numbers"].appendChild( phoneNumber.toXML() );
			} );
			addresses.forEach( function( address:Address, index:int, array:Array ):void
			{
				data["addresses"].appendChild( address.toXML() );
			} );
			instantMessengers.forEach( function( instantMessenger:InstantMessenger, index:int, array:Array ):void
			{
				data["instant-messengers"].appendChild( instantMessenger.toXML() );
			} );
			webAddresses.forEach( function( webAddress:WebAddress, index:int, array:Array ):void
			{
				data["web-addresses"].appendChild( webAddress.toXML() );
			} );
			
			return data;
		}
		
		public function toXMLString():String
		{
			return toXML().toString();
		}
	}
}

/*
<contact-data>
  <email-addresses>
    <email-address>
      <id type="integer">1</id>
      <address>john.doe@example.com</address>
      <location>#{ Work || Home || Other }</location>
    </email-address>
  </email-addresses>
  <phone-numbers>
    <phone-number>
      <id type="integer">2</id>
      <number>555-555-5555</number>
      <location>#{ Work || Mobile || Fax || Pager || Home || Other }</location>
    </phone-number>
    <phone-number>
      <id type="integer">3</id>
      <number>555-666-6666</number>
      <location>Home</location>
    </phone-number>
  </phone-numbers>
  <addresses>
    <address>
      <id type="integer">1</id>
      <city>Sampleville</city>
      <country>United States</country>
      <state>IL</state>
      <street>123 Example Ave</street>
      <zip>55555</zip>
      <location>#{ Work || Home || Other }</location>
    </address>
  </addresses>
  <instant-messengers>
    <instant-messenger>
      <id type="integer">1</id>
      <address>example</address>
      <protocol>#{ 
        AIM || MSN || ICQ || Jabber || Yahoo || Skype || QQ ||
        Sametime || Gadu-Gadu || Google Talk || other
      }</protocol>
      <location>#{ Work || Personal || Other }</location>
    </instant-messenger>
  </instant-messengers>
  <web-addresses>
    <web-address>
      <id type="integer">1</id>
      <address>http://www.example.com</address>
      <location>#{ Work || Personal || Other }</location>
    </web-address>
  </web-addresses>
</contact-data>
*/